package fpt.store.tab;

import java.util.ArrayList;
import java.util.List;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.GoogleMap.OnMarkerClickListener;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.CameraPosition;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;

import fpt.store.main.R;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationManager;
import android.net.Uri;
import android.os.Bundle;
import android.provider.Settings;
import android.util.Log;
import android.widget.Toast;

public class MapFPT extends Activity {
	private GoogleMap googleMap;
	private double lat = 14.823099, lon = 106.629664;
	private float zoom = 6.2f;
	String LOG = "log";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.map);
		try {
			initilizeMap();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void initilizeMap() {
		if (googleMap == null) {
			googleMap = ((MapFragment) getFragmentManager().findFragmentById(
					R.id.map)).getMap();

			CameraPosition cameraPosition = new CameraPosition.Builder()
					.target(new LatLng(lat, lon)).zoom(zoom).build();
			googleMap.animateCamera(CameraUpdateFactory
					.newCameraPosition(cameraPosition));
			googleMap.setMyLocationEnabled(true);
			List<MarkerOptions> listMarker = new ArrayList<MarkerOptions>();

			listMarker.add(new MarkerOptions().position(new LatLng(21.03189,
					105.826382)));
			listMarker.add(new MarkerOptions().position(new LatLng(21.026933,
					105.843985)));
			listMarker.add(new MarkerOptions().position(new LatLng(21.024014,
					105.85377)));
			listMarker.add(new MarkerOptions().position(new LatLng(21.048327,
					105.806547)));
			listMarker.add(new MarkerOptions().position(new LatLng(21.046154,
					105.876542)));
			listMarker.add(new MarkerOptions().position(new LatLng(21.038113,
					105.772257)));
			listMarker.add(new MarkerOptions().position(new LatLng(21.036476,
					105.790041)));
			listMarker.add(new MarkerOptions().position(new LatLng(21.012336,
					105.820162)));
			listMarker.add(new MarkerOptions().position(new LatLng(21.015411,
					105.832919)));
			listMarker.add(new MarkerOptions().position(new LatLng(21.013743,
					105.818992)));
			listMarker.add(new MarkerOptions().position(new LatLng(20.99585,
					105.84972)));
			listMarker.add(new MarkerOptions().position(new LatLng(21.010163,
					105.851587)));
			listMarker.add(new MarkerOptions().position(new LatLng(20.990777,
					105.802627)));
			listMarker.add(new MarkerOptions().position(new LatLng(21.076361,
					105.786318)));
			listMarker.add(new MarkerOptions().position(new LatLng(10.772624,
					106.699637)));
			listMarker.add(new MarkerOptions().position(new LatLng(10.777419,
					106.701965)));
			listMarker.add(new MarkerOptions().position(new LatLng(10.774315,
					106.687996)));
			listMarker.add(new MarkerOptions().position(new LatLng(10.788143,
					106.685743)));
			listMarker.add(new MarkerOptions().position(new LatLng(10.778821,
					106.67972)));
			listMarker.add(new MarkerOptions().position(new LatLng(10.759518,
					106.69858)));
			listMarker.add(new MarkerOptions().position(new LatLng(10.751976,
					106.658497)));
			listMarker.add(new MarkerOptions().position(new LatLng(10.754716,
					106.67885)));
			listMarker.add(new MarkerOptions().position(new LatLng(10.753831,
					106.634861)));
			listMarker.add(new MarkerOptions().position(new LatLng(10.745066,
					106.729323)));
			listMarker.add(new MarkerOptions().position(new LatLng(10.778009,
					106.665691)));
			listMarker.add(new MarkerOptions().position(new LatLng(10.766111,
					106.644289)));
			listMarker.add(new MarkerOptions().position(new LatLng(10.828556,
					106.619156)));
			listMarker.add(new MarkerOptions().position(new LatLng(10.862075,
					106.649748)));
			listMarker.add(new MarkerOptions().position(new LatLng(10.803283,
					106.691733)));
			listMarker.add(new MarkerOptions().position(new LatLng(10.72819,
					106.60754)));
			listMarker.add(new MarkerOptions().position(new LatLng(10.826256,
					106.680529)));
			listMarker.add(new MarkerOptions().position(new LatLng(10.798045,
					106.643852)));
			listMarker.add(new MarkerOptions().position(new LatLng(10.850713,
					106.763076)));
			listMarker.add(new MarkerOptions().position(new LatLng(10.781783,
					106.636036)));
			listMarker.add(new MarkerOptions().position(new LatLng(16.069356,
					108.151441)));
			listMarker.add(new MarkerOptions().position(new LatLng(16.06948,
					108.209983)));
			listMarker.add(new MarkerOptions().position(new LatLng(16.060279,
					108.216726)));
			listMarker.add(new MarkerOptions().position(new LatLng(10.375934,
					105.441219)));
			listMarker.add(new MarkerOptions().position(new LatLng(10.367908,
					105.44786)));
			listMarker.add(new MarkerOptions().position(new LatLng(10.353919,
					107.0833)));
			listMarker.add(new MarkerOptions().position(new LatLng(9.288136,
					105.72232)));
			listMarker.add(new MarkerOptions().position(new LatLng(21.183811,
					106.066678)));
			listMarker.add(new MarkerOptions().position(new LatLng(21.116914,
					105.957934)));
			listMarker.add(new MarkerOptions().position(new LatLng(13.773092,
					109.233093)));
			listMarker.add(new MarkerOptions().position(new LatLng(11.529757,
					106.895304)));
			listMarker.add(new MarkerOptions().position(new LatLng(10.935388,
					108.103861)));
			listMarker.add(new MarkerOptions().position(new LatLng(9.177796,
					105.154942)));
			listMarker.add(new MarkerOptions().position(new LatLng(10.010001,
					105.759034)));
			listMarker.add(new MarkerOptions().position(new LatLng(12.683617,
					108.047345)));
			listMarker.add(new MarkerOptions().position(new LatLng(10.957366,
					106.842743)));
			listMarker.add(new MarkerOptions().position(new LatLng(10.934328,
					107.243025)));
			listMarker.add(new MarkerOptions().position(new LatLng(20.936952,
					106.314258)));
			listMarker.add(new MarkerOptions().position(new LatLng(20.846384,
					106.691306)));
			listMarker.add(new MarkerOptions().position(new LatLng(20.855974,
					106.681612)));
			listMarker.add(new MarkerOptions().position(new LatLng(16.470683,
					107.592791)));
			listMarker.add(new MarkerOptions().position(new LatLng(10.003629,
					105.087533)));
			listMarker.add(new MarkerOptions().position(new LatLng(14.349844,
					108.005395)));
			listMarker.add(new MarkerOptions().position(new LatLng(11.944663,
					108.434447)));
			listMarker.add(new MarkerOptions().position(new LatLng(11.542472,
					107.805318)));
			listMarker.add(new MarkerOptions().position(new LatLng(21.852701,
					106.757267)));
			listMarker.add(new MarkerOptions().position(new LatLng(20.426429,
					106.175496)));
			listMarker.add(new MarkerOptions().position(new LatLng(20.265752,
					105.967665)));
			listMarker.add(new MarkerOptions().position(new LatLng(21.310393,
					105.400916)));
			listMarker.add(new MarkerOptions().position(new LatLng(13.088482,
					109.314192)));
			listMarker.add(new MarkerOptions().position(new LatLng(17.470486,
					106.620115)));
			listMarker.add(new MarkerOptions().position(new LatLng(15.122305,
					108.804339)));
			listMarker.add(new MarkerOptions().position(new LatLng(9.612396,
					105.968906)));
			listMarker.add(new MarkerOptions().position(new LatLng(11.072566,
					106.285252)));
			listMarker.add(new MarkerOptions().position(new LatLng(20.44353,
					106.338214)));
			listMarker.add(new MarkerOptions().position(new LatLng(21.587965,
					105.829448)));
			listMarker.add(new MarkerOptions().position(new LatLng(21.605274,
					105.817455)));
			listMarker.add(new MarkerOptions().position(new LatLng(19.813049,
					105.776767)));
			listMarker.add(new MarkerOptions().position(new LatLng(19.803552,
					105.77641)));
			listMarker.add(new MarkerOptions().position(new LatLng(10.365301,
					106.357092)));
			listMarker.add(new MarkerOptions().position(new LatLng(9.918197,
					106.340839)));

			for (MarkerOptions options : listMarker) {
				// options.icon(BitmapDescriptorFactory.fromResource(R.drawable.markerfpt));
				googleMap.addMarker(options);
			}
			
			LocationManager mlocManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
		    boolean enabled = mlocManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
		    if (enabled) {
				
			} else {
				turnGPSOn(getApplicationContext());
			}

			LocationManager service = (LocationManager) getSystemService(LOCATION_SERVICE);
			Criteria criteria = new Criteria();
			String provider = service.getBestProvider(criteria, false);
			Location location = service.getLastKnownLocation(provider);
			LatLng userLocation = new LatLng(location.getLatitude(),
					location.getLongitude());
			MarkerOptions currentMarker = new MarkerOptions().position(
					userLocation).title("Vị trí hiện tại của quí khách.");
			currentMarker.icon(BitmapDescriptorFactory
					.defaultMarker(BitmapDescriptorFactory.HUE_AZURE));
			googleMap.addMarker(currentMarker);

			if (googleMap == null) {
				Toast.makeText(getApplicationContext(),
						"Sorry! unable to create maps", Toast.LENGTH_SHORT)
						.show();
			}
		}
	}

	@Override
	protected void onResume() {
		super.onResume();
		initilizeMap();
	}

	// private LatLng searchAddress(Context mContext, String mAddress) {
	// GeoPoint gp = null;
	// Geocoder coder = new Geocoder(mContext);
	// List<Address> addresses;
	// try {
	// addresses = coder.getFromLocationName(mAddress, 5);
	// if (addresses == null) {
	// return null;
	// }
	// for (int i = 0; i < 1; i++) {
	// Address location = addresses.get(i);
	// gp = new GeoPoint((int) (location.getLatitude() * 1E6),
	// (int) (location.getLongitude() * 1E6));
	// }
	// } catch (Exception e) {
	// return null;
	// }
	// LatLng point = new LatLng(gp.getLatitudeE6() / 1E6,
	// gp.getLongitudeE6() / 1E6);
	// return point;
	// }

	@SuppressWarnings("deprecation")
	public static void turnGPSOn(Context context) {
		Intent intent = new Intent("android.location.GPS_ENABLED_CHANGE");
		intent.putExtra("enabled", true);
		context.sendBroadcast(intent);

		String provider = Settings.Secure.getString(
				context.getContentResolver(),
				Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
		if (!provider.contains("gps")) { // if gps is disabled
			final Intent poke = new Intent();
			poke.setClassName("com.android.settings",
					"com.android.settings.widget.SettingsAppWidgetProvider");
			poke.addCategory(Intent.CATEGORY_ALTERNATIVE);
			poke.setData(Uri.parse("3"));
			context.sendBroadcast(poke);
		}
	}

	@SuppressWarnings("deprecation")
	public static void turnGPSOff(Context context) {
		String provider = Settings.Secure.getString(
				context.getContentResolver(),
				Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
		if (provider.contains("gps")) { // if gps is enabled
			final Intent poke = new Intent();
			poke.setClassName("com.android.settings",
					"com.android.settings.widget.SettingsAppWidgetProvider");
			poke.addCategory(Intent.CATEGORY_ALTERNATIVE);
			poke.setData(Uri.parse("3"));
			context.sendBroadcast(poke);
		}
	}

}
